package com.xtxb.app.rmq.api;

import com.xtxb.app.rmq.conf.MQServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.ws.rs.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Path("/server")
public class ServerApi extends AbtractAPI{

    @Autowired
    private MQServer server;

    @GET
    @Path("/list")
    @Produces("application/json")
    @Consumes("application/json")
    public List<Map<String,String>> getServers() {
        List<Map<String,String>> values=new ArrayList<>();
        server.getSevers().forEach((name,address)->{
            Map<String,String> map=new HashMap<>();
            map.put("name",name);
            map.put("ip",address.substring(0,address.indexOf(":")));
            map.put("port",address.substring(address.indexOf(":")+1));
            values.add(map);
        });
        return values;
    }

    @POST
    @Path("/add")
    public boolean addServer(@QueryParam("name") String name,@QueryParam("serverAddr") String serverAddr){
        System.out.println(name+"============"+serverAddr);
        server.addServer(name,serverAddr);
        return true;
    }

    @POST
    @Path("/delete")
    public boolean addServer(@QueryParam("name") String name){
        return server.deleteServer(name);
    }
}
